<!--
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Authentication Plugin</title>
<script language="javascript" src="../../yui/yahoo.js"></script>
<script language="javascript" src="../../yui/connection.js"></script>
<script language="javascript" src="../../yui/event.js"></script>
<script language="javascript" src="../../yui/treeview.js"></script>
<script language="javascript" src="../../bin/SEMF.js"></script>
<script language="javascript" src="../../bin/ajax.js"></script>


<link href="../../styles/tree.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>SEMF Authentication Plugin</h1>
<?php
$root_project_home=$_GET['project_home'];
$semf_root = "../../";

$project_home = $semf_root.$root_project_home;

$conf_dir = $project_home."/store/other/plug_ins/Auth/";
$conf_file = $conf_dir."conf.xml";

$doc = new DOMDocument();

$stored_data = array();

if(file_exists($conf_file))
{
	$doc->load($conf_file);
	$root = $doc->documentElement;
	
	$module_elements = $root->childNodes;
	
	foreach($module_elements as $module_element)
	{
		if($module_element->nodeType != XML_ELEMENT_NODE)
		{
			continue;
		}
		$module_name = $module_element->getAttribute("name");
		$stored_data[$module_name] = array();
		
		$page_elements = $module_element->childNodes;
		
		foreach($page_elements as $page_element)
		{
			if($page_element->nodeType != XML_ELEMENT_NODE)
			{
				continue;
			}
			$page_name = $page_element->getAttribute("name");
			$stored_data[$module_name][$page_name] = array();
			
			$access_elements = $page_element->childNodes;
			
			foreach($access_elements as $access_element)
			{
				$access_name = $access_element->getAttribute("name");
				$stored_data[$module_name][$page_name][$access_name] = 1;
			}
		}
	}
}


$files = array();
$d = dir($project_home);
// Loop through all of the files:
$i = 0;
while (false !== ($file = $d->read()))
{
	// If the file is not this file, and does not start with a '.' or '~'
	// and does not end in LCK, then store it for later display:
	if ( ($file{0} != '.') &&
		 ($file{0} != '~') &&
		 (substr($file, -3) != 'php') &&
		 is_dir($project_home.$file) )
	{
		if($file == "store")
		{
			continue;
		}
		$files[$i++] = $file;
	}
}

$modules = array();
$pages = array();

foreach($files as $file)
{
	$module_name = $file;
	$pages[$module_name] = array();
	$modules[count($modules)] = $module_name;
	
	$module_dir = $project_home.$module_name;
	
	$d = dir($module_dir);
	// Loop through all of the files:
	$i = 0;
	while (false !== ($page = $d->read()))
	{
		// If the file is not this file, and does not start with a '.' or '~'
		// and does not end in LCK, then store it for later display:
		if(substr($page, -3) == 'xml')
		{
			if(strtolower($page) == "module.xml")
			{
				continue;
			}
			$pages[$module_name][$i++] = substr($page, 0, strlen($page)-4);
		}
	}	
}

function generate_subtree($stored_data, $id, $user_tables, $module, $page)
{
	$title = "";
	$opened = "false";
	if(is_array($stored_data[$module][$page]))
	{
		$current_accesses = array_keys($stored_data[$module][$page]);
		foreach($current_accesses as $current_access)
		{
			if($title != "")
			{
				$title .= ", ";
			}
			$title .= $current_access;
			//$opened = "true";
		}
	}
	echo "
	var tree_${id} = new YAHOO.widget.TreeView(\"${id}\");
 	var root_${id} = tree_${id}.getRoot(); 
	 
	var componentHTML = \"<i id='root_${id}'>${title}</i>\";
 	var base_${id} = new YAHOO.widget.HTMLNode(componentHTML,
					root_${id}, ${opened}, true);
	";
	
	$i = 0;
	foreach($user_tables as $user_table)
	{
		$component_html = "<span>";
		$component_html .= "<input ";
		if($stored_data[$module][$page][$user_table] == 1)
		{
			$component_html .= "checked=true ";
		}
		$component_html .= " onclick='change_conf(this)' type='checkbox' module='${module}' page='${page}' user='${user_table}'  root='root_${id}'></input>";
		
		$component_html .= $user_table;
		$component_html .= "</span>";
		
		echo "var componentHTML = \"${component_html}\";\n";
		
		echo "var base_${id}_${i} = new YAHOO.widget.HTMLNode(componentHTML,
				base_${id}, false, true);
				"; 
		$i ++;
	}
	
}
	
?>

<script language="javascript">
function init()
{
 var confTree = new YAHOO.widget.TreeView("confTree");
 var root = confTree.getRoot();
 
 var componentHTML = "<i>Select Access Control.</i>";

 var base = new YAHOO.widget.HTMLNode(componentHTML,
					root, true, true);
					
 <?php 
 
 	$sys_module_xml = $project_home."/System/module.xml";
		
	//load the table names User inherited..
	$user_tables = array();
	$doc = new DOMDocument();
	$doc->load($sys_module_xml);
	
	$child_nodes = $doc->documentElement->childNodes;
	foreach($child_nodes as $child_node)
	{
		if($child_node->nodeType != XML_ELEMENT_NODE)
		{
			continue;
		}
		if($child_node->getAttribute("name") == "User") //get the default users
		{
			$child_tables = $child_node->childNodes;
			foreach($child_tables as $child_table)
			{
				if($child_table->nodeType != XML_ELEMENT_NODE)
				{
					continue;
				}
				if($child_table->tagName == "child")
				{
					if($child_table->firstChild)
					{
						 $table_name = trim($child_table->firstChild->nodeValue);
						 //$table_name = str_replace( ".", "_", $table_name);
						 $user_tables[count($user_tables)] = $table_name;
					}
				}
			}
		}
	}
	
	
 	foreach($modules as $module)
	{
		$component_html = "<b>${module}</b>";
		
		echo 
			"var base_${module} = new YAHOO.widget.HTMLNode(\"${component_html}\",
					base, true, true);
			";
		$module_pages = $pages[$module];
		foreach($module_pages as $page)
		{
			$component_html = "<div style='width:700px'>";
			$link = $semf_root."page_designer/index.php?";
			$link .= "result_dir=../".$root_project_home."/".$module."/&";
			$link .= "page_xml=../".$root_project_home."/".$module."/".$page.".xml&";
			$link .= "project_home=../".$root_project_home."/&";
			$component_html .= "<div style='float:left;'>";
			$component_html .= " <a href='${link}'>${page}</a>";
			$component_html .= "</div>";
			$component_html .= "<div style='float:left; width:30px'>&nbsp;";
			$component_html .= "</div>";
			
			$div_id = $module."_".$page;
			$component_html .= "<div style='float:left;' id='${div_id}'>";
			$component_html .= "</div>";
			$component_html .= "</div>";
			
			echo "var componentHTML = \"${component_html}\";\n";
			
			echo "var base_${module}_${page} = new YAHOO.widget.HTMLNode(componentHTML,
					base_${module}, true, true);
					";
			generate_subtree($stored_data, $div_id, $user_tables, $module, $page);
		}
	}
 ?>
 
 confTree.draw();
 
 <?php
  	foreach($modules as $module)
	{
		$module_pages = $pages[$module];
		foreach($module_pages as $page)
		{
			$div_id = $module."_".$page;
			echo "tree_${div_id}.draw()\n";
			
		}
	}
 ?>
}

function change_conf(ctrl)
{
	var module = ctrl.getAttribute("module");
	var page = ctrl.getAttribute("page");
	var user = ctrl.getAttribute("user");
	var checked = ctrl.getAttribute("checked");
	var rootId = ctrl.getAttribute("root");
	
	var rootEle = document.getElementById(rootId);
	var html = rootEle.innerHTML;
	var newRootText = user;
	startIndex = html.indexOf(newRootText);
	if(startIndex >= 0) //remove it if exists.
	{
		if(startIndex+newRootText.length == html.length)
		{
			firstLimit = startIndex -2; //excluded the last comma
		}
		else
		{
			firstLimit = startIndex;
		}
		earlyText = html.substr(0, firstLimit);
		endText = html.substr(startIndex+newRootText.length + 1); //plus the comma
		html = earlyText + endText;
	}
	else //just add
	{
		if(html != "")
		{
			html += ", ";
		}
		html += newRootText;
	}
	
	rootEle.innerHTML = html;
	
	SEMF.ajax.request("xml_builder.php",
			{
			  success: SEMF.ajax.handleSuccess,
			  failure: SEMF.ajax.handleFailure,
			  timeout: 5000,
			  params: [
			  	'module', module,
				'page', page,
				'user', user,
				'checked', checked,
				'project_home', '<?php echo $project_home?>'
			   ]
			});
}
</script>


<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
	<td width="106" height="19">&nbsp;</td>
    <td width="636" valign="top">
	<div id="confTree">
	</div>
	</td>
  </tr>
  <tr height="20px">
  </tr>
  <tr>
  	<td>
	</td>
	<td>
		<a href="generate.php?project_home=<?php echo $root_project_home; ?>">Generate</a>
	</td>
  </tr>
</table>
<script language="javascript">
init();
</script>
</body>
</html>
